/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.babystar.io
 *
 * 版权所有，侵权必究！
 */

package io.babystar.modules.sys.controller;

import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller公共组件
 *
 * @author Mark sunlightcs@gmail.com
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TeacherService teacherService;
	
	protected SysUserEntity getUser() {
		return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}

	/**
	 * 获取session
	 *
	 * @return
	 */
	public Session getSession() {
		Session session = ShiroUtils.getSession();
		return session;
	}

	/**
	 * 获取教练所属门店
	 * @return
	 */
	public Integer getTeacherShopId(){
		SysUserEntity user = this.getUser();
		int type = user.getType();
		//2店长 3教练
		if(type==2 || type==3){
			Long teacherId = user.getParentTeacherId();
			TeacherEntity teacherEntity = teacherService.getById(teacherId);
			return teacherEntity.getShopId();
		}
		return null;
	}
}
